﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Cloak.Controls
{
	/// <summary>
	/// A <see cref="System.Windows.Controls.ContentControl"/> which selects a content template based on the content
	/// </summary>
	public abstract class DataTemplateSelector : ContentControl
	{
		/// <summary>
		/// Handles changes in the <see cref="Content"/> property
		/// </summary>
		/// <param name="oldContent">The content which was replaced by the new content</param>
		/// <param name="newContent">The content which replaced the old content</param>
		protected override void OnContentChanged(object oldContent, object newContent)
		{
			SelectContentTemplate();

			base.OnContentChanged(oldContent, newContent);
		}

		/// <summary>
		/// Selects a content template based on the content
		/// </summary>
		protected virtual void SelectContentTemplate()
		{
			ContentTemplate = GetTemplate(Content);
		}

		/// <summary>
		/// When implemented by a derived class, gets the template which displays the specified object
		/// </summary>
		/// <param name="item">The object which serves as this control's content</param>
		/// <returns>The template which displays the specified object</returns>
		protected abstract DataTemplate GetTemplate(object item);
	}
}